home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Is This Bad Coding Practice?
- Date: Fri, 29 Mar 96 21:31:55 GMT
- Organization: none
- Message-ID: <828135115snz@genesis.demon.co.uk>
- References: <4jgnt2$9d1@loki.tor.hookup.net>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4jgnt2$9d1@loki.tor.hookup.net>
- Rajendra_Singh@msn.com "Rajendra Singh" writes:
-
- >int main(int argc, char *argv[])
- > {
- > char string[128];
- > char *func1(void);
- >
- > strcpy(string, func1());
- > return 0;
- > }
- >
- >char *func1(void)
- > {
- > char test[100];
- >
- > sprintf(test, "Test: %d", 1);
- > return test;
- > }
- >
- >... since I am using the value returned from func1() immediately (in
- >main()), is this reliable?
-
- No, as soon as func1 returns test doesn't exist any more so you cannot
- safely access it (doing so results in undefined behaviour). Don't
- do it, even if it seems to work on your system - it will bite you, sooner
- rather than later if you're lucky.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-